<?php
class TActiveForm extends CActiveForm
{
    protected function registerClientScript()
    {
        $cs = Yii::app()->getClientScript();
        $cs->registerScriptFile(Yii::app()->theme->baseUrl.'/js/popover.js',CClientScript::POS_END);
    }
    public function textFieldTb($model,$attribute,$htmlOptions=array())
    {
       if(!isset($htmlOptions['class']))
       {
           if(!isset($htmlOptions['size']))
           {
               $htmlOptions['size'] = 50;
           }
           if(!isset($htmlOptions['maxlength']))
           {
               $htmlOptions['maxlength'] = 50;
           }
           $htmlOptions['class'] = 'form-control';
           return  CHtml::activeTextField($model,$attribute,$htmlOptions);
       }

        return  CHtml::activeTextField($model,$attribute,$htmlOptions);

    }
    public function textAreaTb($model,$attribute,$htmlOptions=array())
    {
        if(!isset($htmlOptions['class']))
        {
            if(!isset($htmlOptions['rows']))
            {
                $htmlOptions['rows'] = 3;
            }
            if(!isset($htmlOptions['cols']))
            {
                $htmlOptions['cols'] = 80;
            }
            $htmlOptions['class'] = 'form-control';
            return  CHtml::activeTextArea($model,$attribute,$htmlOptions);
        }

        return  CHtml::activeTextArea($model,$attribute,$htmlOptions);

    }

    public function labelTb($model,$attribute,$htmlOptions=array())
    {
        $label =  CHtml::openTag('span',array('class'=>'input-group-addon','style'=>'width:100px'));
        $label .= CHtml::activeLabelEx($model,$attribute,$htmlOptions);
        $label .= CHtml::closeTag('span');
        return $label;

    }
    public function passwordFieldTb($model,$attribute,$htmlOptions=array())
    {
        if(!isset($htmlOptions['class']))
        {
            if(!isset($htmlOptions['size']))
            {
                $htmlOptions['size'] = 50;
            }
            if(!isset($htmlOptions['maxlength']))
            {
                $htmlOptions['maxlength'] = 50;
            }
            $htmlOptions['class'] = 'form-control';
            return  CHtml::activePasswordField($model,$attribute,$htmlOptions);
        }

        return  CHtml::activeTextField($model,$attribute,$htmlOptions);
    }

    public function emailFieldTb($model,$attribute,$htmlOptions=array())
    {
        if(!isset($htmlOptions['class']))
        {
            $htmlOptions['class'] = 'form-control';
        }
        return  CHtml::activeEmailField($model,$attribute,$htmlOptions);
    }

    //Статический метод генерирует кнопку с иконкой сохранить или создать
    public static function submitButtonTb($model,$htmlOptions=array())
    {
        $moduleName  = strtolower(Yii::app()->controller->module->id);
        $value = $model->isNewRecord ? Yii::t($moduleName .'Module.base','Create') : Yii::t($moduleName .'Module.base','Save');
        if(!isset($htmlOptions['class']))
        {
            $htmlOptions['class'] = 'btn btn-default';
        }
        if(isset($htmlOptions['data-icon']))
        {
            $button = CHtml::openTag('button',array('class'=>$htmlOptions['class'],'type'=>'submit'));
            $button .= CHtml::tag('i',array('class'=>$htmlOptions['data-icon']),'');
            $button .= ' '.$value;
            $button .= CHtml::closeTag('button');

            return $button;
        }
        return  CHtml::submitButton($value,$htmlOptions);
    }

    //Генерация кнопки Tb
    public static function buttonTb($label,$htmlOptions=array())
    {
        $moduleName = strtolower(Yii::app()->controller->module->id);
        $value = Yii::t($moduleName .'Module.base',$label);
        if(!isset($htmlOptions))
        {
            $htmlOptions = array();
        }
        if(!isset($htmlOptions['class']))
        {
            $htmlOptions['class'] = 'btn btn-default';
        }

        if(isset($htmlOptions['data-icon']))
        {
            $button = CHtml::openTag('button',array('class'=>$htmlOptions['class'],'type'=>'submit'));
            $button .= CHtml::tag('i',array('class'=>$htmlOptions['data-icon']),'');
            $button .= ' '.$value;
            $button .= CHtml::closeTag('button');

            return $button;
        }

        return  CHtml::submitButton($value,$htmlOptions);
    }

    /**
     * Генерирует кнопку отмены с иконкой
     * Если $path установлена, то регисрируется скрипт
     * нажатия данной кнопки
     * @param null $path
     * @param array $htmlOptions
     * @return string
     */
    public static function cancelButtonTb($path = null,$htmlOptions = array())
    {
        return THtml::buttonTb('Back',$path,$htmlOptions);
    }

    public function activeFileField($model,$attribute,$htmlOptions=array())
    {
        return CHtml::activeFileField($model,$attribute,$htmlOptions);
    }

    public function textAreaEditor($model,$attribute,$htmlOptions=array())
    {
        THtml::initEditor($htmlOptions);
        return THtml::activeEditorField($model,$attribute,$htmlOptions);
    }

    public function dropDownListTb($model,$attribute,$data = array(),$htmlOptions = array())
    {
       return THtml::activeDropDownListTb($model,$attribute,$data,$htmlOptions);
    }
}
?>
